# -*- coding: utf-8 -*-
# Project : flacon
# File : about.py
# (c) Alexander Sokolov sokoloff.a@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.



#import preferences
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import pyqticonloader
import flacon
from aboutDialog_ui import *
#from flaconglobal import *
import translators_

class AboutDialog(QtGui.QDialog, Ui_aboutDialog):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, parent):
        QDialog.__init__(self, parent)
        self.setupUi(self)

        # Strings ::::::::::::::::::::::::::::::::::::::::::::::::::
        windowTitle = self.tr("About Flacon")
        description = self.tr("Extracts individual tracks from one big audio file containing the entire album.",)
        version     = self.tr("Version: %1").arg(flacon.FLACON_VERSION)
        webSiteStr  = self.tr("Web site:")
        licenseStr  = self.tr("License:")


        authors     = [
                ("Alexander Sokolov",
                    "mailto:sokoloff.a@gmail.com",
                    ''
                )
            ]


        helpers     = [
                ('Artem Aleksuk',
                    'mailto:h31mail@yandex.ru',
                    self.tr('WavPack support patch.')
                ),

                ('Charles Barcza',
                    'mailto:kbarcza@blackpanther.hu',
                    self.tr('Application icon, Packaging.')
                ),

                ('Kyrill Detinov',
                    'mailto:lazy.kent@opensuse.org',
                    self.tr('Packaging, testing.')
                ),

                ('Alain-Olivier Breysse',
                    'mailto:yahoe.001@gmail.com',
                    self.tr('Improvements in the UI.')
                ),

            ]

        programs = [
                ('shntool',             'http://etree.org/shnutils/shntool/', ''),
                ('flac and metaflac',   'http://flac.sourceforge.net/', ''),
                ('mac',                 ['http://etree.org/shnutils/shntool/support/formats/ape/unix/', 'http://www.monkeysaudio.com'], ''),
                ('oggenc',              'http://www.xiph.org/', ''),
                ('LAME',                'http://lame.sourceforge.net/', ''),
                ('WavPack',             'http://www.wavpack.com/', ''),
                ('VorbisGain',          'http://sjeng.org/vorbisgain.html', ''),
                ('MP3Gain',             'http://mp3gain.sourceforge.net/', ''),
                ('ttaenc',              'http://tta.sourceforge.net/', ''),
                ('FAAC',                'http://www.audiocoding.com/', ''),
            ]


        copyright  = "&copy; Alexander Sokolov, 2009-2011"
        license    = "<a href=http://www.fsf.org/licensing/licenses/gpl.html>GNU General Public License Version 3</a>"
        siteUrl    = "http://code.google.com/p/flacon"

        # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        css = QString('''<style type="text/css">
                            .name { font-weight: bold;  font-size: 12pt; }
                            .ver  { font-weight: bold;  qfont-size: 8pt; }
                            a     { white-space: nowrap ; }
                        </style>''')


        self.setWindowTitle(windowTitle)

        logo = pyqticonloader.icon('flacon', [48]).pixmap(48, 48)
        self.logoLabel.setPixmap(logo)


        self.titleLabel.setText(css + ("<div class=name>Flacon</div><div class=ver>%s</div><p>" % version))


        aboutText = "%s<p>" % description   + \
                    "<br>%s"    % copyright     + \
                    "<hr>"                  + \
                    "%s <a href='%s'>%s</a><br>" % (webSiteStr, siteUrl, siteUrl) + \
                    "%s %s" % (licenseStr, license)
        self.aboutLabel.setText(css+aboutText)


        self.authorsEdit.viewport().setAutoFillBackground(False)
        self.authorsEdit.setText(css + self._getListString(authors))

        self.thanksEdit.viewport().setAutoFillBackground(False)
        self.thanksEdit.setHtml(css + self._getListString(helpers))

        self.translationsEdit.viewport().setAutoFillBackground(False)
        self.translationsEdit.setHtml(css + translators_.TRANSLATORS_TEXT)

        self.programsEdit.viewport().setAutoFillBackground(False)
        self.programsEdit.setHtml(css + self.tr('Flacon uses external programs. Many thanks to their authors!') + '<p>' + self._getListString(programs))


   #****************************************************************
   #
   #****************************************************************
    def _getListString(self, items):
        result = ''
        for (name, urls, role) in sorted(items, key=lambda item: item[0]):
            result += "<b>%s</b>" % name

            if urls:
                if not isinstance(urls, list):
                    urls = [urls]

                for url in urls:
                    text = QString(url).remove('mailto:')
                    result +="<br><a href='%s'>%s</a>" % (url, text)

            if role:
                result +="<br>%s" % role

            result += "<p>"

        return result


